package mx.com.mindbits.bandpay;

import java.util.List;

import mx.com.mindbits.bandpay.adapters.TransactionItemAdapter;
import mx.com.mindbits.bandpay.dao.BandPayDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.dao.transactions;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class TransactionHistoryActivity extends SherlockActivity {

	private BandPayDAO bAndPayDAO;
	private ListView transListView;
	private List<transactions> transDataList;
	private transactions _transaction;
	private ActionBar actionBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_trans_history);
		generateTransactionHistoryList();	
		//////////action bar settings ///////////////////////
		setSettingActionBar();
	}
	
	public void generateTransactionHistoryList(){
		
		bAndPayDAO = new BandPayDAO(getBaseContext());
		transListView = (ListView) findViewById(R.id.list_transactions);
		transDataList = bAndPayDAO.getAllTransactionsDESC();
		if(transDataList != null && !transDataList.isEmpty()){
			TransactionItemAdapter adapter = new TransactionItemAdapter
					(getBaseContext(),
					R.layout.trans_item_view, 
					transDataList, R.id.tv_descripcion,
					R.id.tv_fecha, R.id.tv_hora, R.id.id_tv_estado);
			transListView.setAdapter(adapter);
			transListView.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> adapter, View view,
						int position, long id) {
					_transaction = transDataList.get(position);
					showTransactionDetail(_transaction.get_id_transaccion());
				}});
		}else{
			Log.i(ConstantsValues.TAG_TRANS_HISTORY, "No hay datos que mostrar");
		}
	
	}
	public void showTransactionDetail(long idTransaction){
		Intent toDetail = new Intent(getBaseContext(), TransactionDetailActivity.class);
		toDetail.putExtra("idTransaction", idTransaction);
		startActivity(toDetail);
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		bAndPayDAO.closeDataBase();
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case android.R.id.home:
			Intent intent = new Intent(this, MainMenuActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		}
		return true;
	}
	
	public void setSettingActionBar(){

		actionBar = getSupportActionBar();
		actionBar.setTitle("");
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);

	}	
}
